package com.ruicar.afs.cloud.manage.penaltyexempt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.service.AfsUser;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.FeeTypeEnum;
import com.ruicar.afs.cloud.manage.common.entity.ManageCostExemptConfig;
import com.ruicar.afs.cloud.manage.common.entity.ManagePenaltyExempt;
import com.ruicar.afs.cloud.manage.common.entity.ManagePenaltyExemptDetails;
import com.ruicar.afs.cloud.manage.common.service.ManageCostExemptConfigService;
import com.ruicar.afs.cloud.manage.common.service.ManagePenaltyExemptDetailsService;
import com.ruicar.afs.cloud.manage.common.service.ManagePenaltyExemptService;
import com.ruicar.afs.cloud.manage.penaltyexempt.condition.ManagePenaltyExemptCondition;
import com.ruicar.afs.cloud.manage.workflow.processor.ApproveTaskProcessor;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: xudongdong
 * @date: 2020/8/5
 * @description: 单笔罚息信息查询
 */
@Slf4j
@RestController
@RequestMapping("/findPenaltyExempt")
@AllArgsConstructor
public class ManagePenaltyExemptController {

    private final ApproveTaskProcessor approveTaskProcessor;
    private final ManagePenaltyExemptService managePenaltyExemptService;
    private final ManageCostExemptConfigService manageCostExemptConfigService;
    private final ManagePenaltyExemptDetailsService managePenaltyExemptDetailsService;

    @ApiOperation(value = "查询单笔罚息列表数据")
    @PostMapping("/findPenaltyExemptList")
    public IResponse findPenaltyExemptList(@RequestBody QueryCondition<ManagePenaltyExemptCondition> condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        condition.getCondition().setProcessPerson(SecurityUtils.getUser().getUsername());
        IPage list = managePenaltyExemptService.getPenaltyExemptList(page, condition.getCondition());
        list.setRecords(approveTaskProcessor.handle(list.getRecords()));
        return new IResponse().setData(list);
    }

    @ApiOperation(value = "查询单笔罚息页面详细数据")
    @PostMapping("/findPenaltyExemptDetails")
    public IResponse<ManagePenaltyExempt> findPenaltyExemptDetails(@RequestParam("id") String id) {
        ManagePenaltyExemptCondition condition = new ManagePenaltyExemptCondition();
        ManagePenaltyExempt managePenaltyExempt = managePenaltyExemptService.getById(id);
        BeanUtils.copyProperties(managePenaltyExempt, condition);
        List<ManagePenaltyExemptDetails> managePenaltyExemptDetails = managePenaltyExemptDetailsService.list(Wrappers.<ManagePenaltyExemptDetails>lambdaQuery()
                .eq(ManagePenaltyExemptDetails::getPenaltyId, id));
        for (ManagePenaltyExemptDetails penaltyExemptDetails : managePenaltyExemptDetails) {
            penaltyExemptDetails.setCustName(condition.getCustName());
        }
        condition.setManagePenaltyExemptDetails(managePenaltyExemptDetails);
        return new IResponse<ManagePenaltyExempt>().setData(condition);
    }

    @ApiOperation(value = "保存单笔罚息信息")
    @PostMapping("/savePenaltyExempt")
    public IResponse savePenaltyExempt(@RequestBody ManagePenaltyExemptCondition managePenaltyExempt) {

        managePenaltyExemptService.savePenaltyExempt(managePenaltyExempt);

        return new IResponse().setData(managePenaltyExempt);
    }

    @PostMapping("/submitPenaltyExempt")
    @ApiOperation(value = "提交单笔罚息业务信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse submitPenaltyExempt(@RequestBody ManagePenaltyExemptCondition managePenaltyExempt) {
        managePenaltyExemptService.submitPenaltyExempt(managePenaltyExempt);
        return new IResponse<Boolean>().setData(true).setMsg("流程提交成功！");
    }

    @PostMapping("/approvePenaltyExempt")
    @ApiOperation(value = "流程审批操作")
    @Transactional(rollbackFor = Exception.class)
    public IResponse approvePenaltyExempt(@RequestBody ManagePenaltyExemptCondition managePenaltyExempt) {
        Boolean flag = managePenaltyExemptService.approvePenaltyExempt(managePenaltyExempt);
        if (flag) {
            AfsUser user = SecurityUtils.getUser();
            Date newDate = new Date();
            String year = new SimpleDateFormat("yyyy").format(newDate.getTime());
            ManageCostExemptConfig manageCostExemptConfig = manageCostExemptConfigService.getOne(Wrappers.<ManageCostExemptConfig>lambdaQuery()
                    .eq(ManageCostExemptConfig::getDeptId, user.getDeptId())
                    .eq(ManageCostExemptConfig::getYear, year)
                    .eq(ManageCostExemptConfig::getCostType, FeeTypeEnum.penalty));
            if (manageCostExemptConfig != null) {
                manageCostExemptConfig.setSurplusLimitAmt(manageCostExemptConfig.getSurplusLimitAmt().subtract(managePenaltyExempt.getExemptionAmt()));
            }
        }
        return new IResponse<Boolean>().setData(flag).setMsg("流程提交成功！");
    }

    @Transactional
    @SysLog("删除单笔罚息信息")
    @ApiOperation(value = "通过id删除")
    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    public IResponse<Boolean> delById(@PathVariable String id) {
        //删除单笔罚息详细信息表
        managePenaltyExemptService.removeById(id);
        //删除每期罚息详细信息表
        managePenaltyExemptDetailsService.remove(Wrappers.<ManagePenaltyExemptDetails>lambdaQuery()
                .eq(ManagePenaltyExemptDetails::getPenaltyId, id));
        return new IResponse<Boolean>().setMsg("删除成功");
    }

    @ApiOperation(value = "查询单笔合同限额数据")
    @PostMapping("/getLimitAmt")
    public IResponse getLimitAmt() {

        ManageCostExemptConfig manageCostExemptConfig = manageCostExemptConfigService.getOne(Wrappers.<ManageCostExemptConfig>lambdaQuery()
                .eq(ManageCostExemptConfig::getCostType, FeeTypeEnum.penalty)
                .eq(ManageCostExemptConfig::getUser, SecurityUtils.getUser().getUsername()));

        return new IResponse().setData(manageCostExemptConfig);
    }

    @ApiOperation(value = "查询总体限额数据")
    @PostMapping("/getCostExempt")
    public IResponse getCostExempt() {

        Date newDate = new Date();
        String year = new SimpleDateFormat("yyyy").format(newDate.getTime());
        ManageCostExemptConfig manageCostExemptConfig = manageCostExemptConfigService.getOne(Wrappers.<ManageCostExemptConfig>lambdaQuery()
                .eq(ManageCostExemptConfig::getDeptId, SecurityUtils.getUser().getDeptId())
                .eq(ManageCostExemptConfig::getYear, year)
                .eq(ManageCostExemptConfig::getCostType, FeeTypeEnum.penalty));

        return new IResponse().setData(manageCostExemptConfig);
    }

}
